package com.zhang.server.controller;

import com.zhang.server.common.Result;
import com.zhang.server.controller.request.UserPageRequest;
import com.zhang.server.entity.User;
import com.zhang.server.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

    @GetMapping("/list")
    public Result list(){
        return Result.success(userService.list());
    }

    @GetMapping("/page")
    public Result page(UserPageRequest userPageRequest){
        if(userPageRequest.getPageNum() == null){
            userPageRequest.setPageNum(1);
        }

        if(userPageRequest.getPageSize() == null){
            userPageRequest.setPageSize(10);
        }
        return Result.success(userService.page(userPageRequest));
    }

    @PostMapping("/save")
    public Result save(@RequestBody User user){
        userService.save(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return userService.getById(id);
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        userService.delete(id);
        return Result.success();
    }

}
